package com.longteng.diamond.controller;

import com.alibaba.fastjson.JSON;
import com.longteng.diamond.domain.InterfaceTest;
import com.longteng.diamond.domain.ProjectShareNode;
import com.longteng.diamond.domain.User;
import com.longteng.diamond.po.RequestResult;
import com.longteng.diamond.po.TablePage;
import com.longteng.diamond.service.InterfaceTestService;
import com.longteng.diamond.service.impl.ProjectHostInterface;
import com.longteng.diamond.util.ValidUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator
 * DATE : 2018/6/4
 * TIME : 23:07
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@Slf4j
@Controller
@RequestMapping("/interfaceTestController")
public class InterfaceTestController {
    @Autowired
    InterfaceTestService interfaceTestService;
    @Autowired
    ProjectHostInterface projectHostInterface;
    @Autowired
    ValidUtil validUtil;

    @RequestMapping("/interfaceTestPage")
    public String interfaceTestPage() {
        return "page/interfaceTest/interfaceTestPage";
    }

    @RequestMapping("/getInterfaceTest")
    public @ResponseBody
    TablePage getInterfaceTest(InterfaceTest interfaceTest) {
        TablePage tablePage = new TablePage();
        try {
            tablePage = interfaceTestService.getInterfaceTest(interfaceTest);
        } catch (Exception e) {
            log.error("获取用户错误:", e);
        }
        return tablePage;
    }

    @RequestMapping("/addInterfaceTest")
    public ModelAndView addInterfaceTest(@Valid InterfaceTest interfaceTest, BindingResult bindingResult) {
        ModelAndView modelAndView = new ModelAndView();
        if (!validUtil.checkParam(interfaceTest.getClass().getDeclaredFields(), bindingResult, modelAndView)) {
            modelAndView.setViewName("page/interfaceTest/editInterfaceTestPage");
            return modelAndView;
        }
        try {
            interfaceTestService.addInterfaceTest(interfaceTest);
        } catch (Exception e) {
            log.error("新增接口错误", e);
            modelAndView.setViewName("/interfaceTestController/editInterfaceTest");
            modelAndView.addObject("errorMsg", e.toString());
            return modelAndView;
        }
        modelAndView.setViewName("redirect:/interfaceTestController/interfaceTestPage");
        return modelAndView;
    }


    @RequestMapping("/updateInterfaceTest")
    public ModelAndView updateInterfaceTest(@Valid InterfaceTest interfaceTest, BindingResult bindingResult) {
        ModelAndView modelAndView = new ModelAndView();
        if (!validUtil.checkParam(interfaceTest.getClass().getDeclaredFields(), bindingResult, modelAndView)) {
            modelAndView.setViewName("page/interfaceTest/editInterfaceTestPage");
            return modelAndView;
        }
        try {
            interfaceTestService.updateInterfaceTest(interfaceTest);
        } catch (Exception e) {
            log.error("更新接口错误", e);
        }
        modelAndView.setViewName("redirect:/interfaceTestController/interfaceTestPage");
        return modelAndView;
    }

    @RequestMapping("/editInterfaceTestPage")
    public ModelAndView editInterfaceTestPage(InterfaceTest interfaceTest) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/interfaceTest/editInterfaceTestPage");
        if (null != interfaceTest.getInterfaceId() && interfaceTest.getInterfaceId() > 0) {
            interfaceTest = interfaceTestService.findInterfaceTestById(interfaceTest.getInterfaceId());
        }
        List<ProjectShareNode> hosts=projectHostInterface.getHostForInterface(interfaceTest.getProjectId());
        modelAndView.addObject("hosts", hosts);
        modelAndView.addObject("interfaceTest", interfaceTest);
        return modelAndView;
    }

    @RequestMapping("/deleteInterfaceTest")
    public @ResponseBody
    RequestResult deleteInterfaceTest(Integer interfaceTestId) {
        RequestResult requestResult = new RequestResult();
        try {
            interfaceTestService.deleteInterfaceTestById(interfaceTestId);
            requestResult.setSuccess(true);
        } catch (Exception e) {
            requestResult.setSuccess(false);
            requestResult.setMsg("删除接口异常,请联系管理员");
            log.error("删除接口异常", e);
        }
        return requestResult;
    }

    @RequestMapping("/bindHeaders")
    public @ResponseBody RequestResult bindHeaders(String headers, Integer[] interfaceIds){
        RequestResult requestResult = new RequestResult();
        try {
            interfaceTestService.bindHeaders(headers,interfaceIds);
            requestResult.setSuccess(true);
            requestResult.setMsg("绑定header成功");
        } catch (Exception e) {
            requestResult.setSuccess(false);
            requestResult.setMsg("绑定header错误,请联系管理员!");
            log.error("绑定header错误",e);
        }
        return requestResult;
    }
}
